import type { FormSchemaGetter, VbenFormSchema } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { getPopupContainer } from '@vben/utils';

import { z } from '#/adapter/form';
import { renderDictTag } from '#/utils/render';

/**
 * 性别选项
 */
export const sexOptions = [
  {
    color: 'blue',
    label: '男',
    value: 1,
    dictLabel: '男',
    dictValue: 1,
  },
  {
    color: 'pink',
    label: '女',
    value: 0,
    dictLabel: '女',
    dictValue: 0,
  },
];

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'nickName',
    label: '用户昵称',
  },
];

export const columns: VxeGridProps['columns'] = [
  {
    title: '昵称',
    field: 'nickName',
    minWidth: 120,
  },
  {
    title: '手机号',
    field: 'mobile',
    minWidth: 130,
  },
  {
    title: '邮箱',
    field: 'email',
    minWidth: 120,
  },
  {
    field: 'avatar',
    title: '头像',
    slots: { default: 'avatar' },
    minWidth: 100,
  },
  {
    title: '性别',
    field: 'sex',
    minWidth: 80,
    slots: {
      default: ({ row }) => {
        return renderDictTag(row.sex, sexOptions);
      },
    },
  },
  {
    title: '生日',
    field: 'birthday',
    minWidth: 150,
  },
  {
    title: 'openId',
    field: 'openId',
    minWidth: 250,
  },
  {
    title: '联系人名称',
    field: 'contactName',
    minWidth: 160,
  },
  {
    title: '联系人号码',
    field: 'contactPhone',
    minWidth: 130,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    resizable: false,
    width: 'auto',
  },
];

export const modalSchema = (
  handleUpload: (file: File) => void,
  handleRemove: () => void,
): VbenFormSchema[] => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
    label: '用户ID',
  },
  {
    component: 'Upload',
    componentProps: {
      // 更多属性见：https://ant.design/components/upload-cn
      accept: '.png,.jpg,.jpeg',
      // 自动携带认证信息
      // customRequest: uploadApi,
      disabled: false,
      maxCount: 1,
      multiple: false,
      showUploadList: true,
      // 上传列表的内建样式，支持四种基本样式 text, picture, picture-card 和 picture-circle
      listType: 'picture-card',
      beforeUpload: (file: File) => {
        handleUpload(file);
        return false;
      },
      onRemove: (_file: File) => {
        handleRemove();
      },
    },
    fieldName: 'avatar',
    label: '头像',
    renderComponentContent: () => {
      return {
        default: () => '上传头像',
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'nickName',
    label: '昵称',
    rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: sexOptions,
    },
    fieldName: 'sex',
    label: '性别',
    defaultValue: 1,
  },
  {
    component: 'Input',
    fieldName: 'mobile',
    label: '手机号码',
    rules: z
      .string()
      .regex(/^1[3,4578]\d{9}$/, { message: '请输入正确的手机号' })
      .optional()
      .or(z.literal('')),
  },
  {
    component: 'DatePicker',
    fieldName: 'birthday',
    label: '生日',
    componentProps: {
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
  },
  {
    component: 'Input',
    fieldName: 'contactName',
    label: '联系人名称',
  },
  {
    component: 'Input',
    fieldName: 'contactPhone',
    label: '联系人号码',
  },
];

export const passwordModalSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'userId',
    label: '用户ID',
  },
  {
    component: 'Input',
    fieldName: 'password',
    componentProps: {
      type: 'password',
    },
    label: '新密码',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'confirmPassword',
    componentProps: {
      type: 'password',
    },
    label: '确认密码',
    rules: z.string().min(1, { message: '请输入确认密码' }),
  },
];
